package com.lcxw.hxzbadmin.controller.duanweicontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.Duanwei;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.DuanweiService;
import com.lcxw.hxzbcommon.common.ObsUtil;

/**
 * @author Administrator
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("admin/duanwei")
public class DuanweiController {

    @Resource
    DuanweiService duanweiService;

    private ObsUtil obsUtil = new ObsUtil();

    /**
     * 1、获取所有段位接口
     *
     */
    @PostMapping("/getAll")
    public Response getAllDuanwei(@RequestBody JSONObject requestParams) {
        log.info("admin {} get all duanweis", requestParams.getString("adminName"));
        List<Duanwei> duanweis = duanweiService.selectAll();
        Map<String, Object> data = new HashMap<>();
        data.put("duanweis", duanweis);
        return Response.ok(data);
    }

    /**
     * 2、修改/添加所有段位接口
     */
    @PostMapping("/mod")
    public Response modDuanwei(@RequestBody ModReqBean modReqBean) {
        log.info("admin {} mod duanwei info.", modReqBean.getAdminName());
        List<Duanwei> duanweis = modReqBean.getDuanweis();
        for (Duanwei duanwei : duanweis) {
            if (null == duanwei.getId() || null == duanweiService.selectByPrimaryKey(duanwei.getId())) {
                duanweiService.insert(duanwei);
            } else {
                duanweiService.updateByPrimaryKey(duanwei);
            }
        }
        return Response.ok();
    }

    /**
     * 3、删除一个段位
     */
    @PostMapping("/delete")
    public Response deleteDuanwei(@RequestBody JSONObject requestParams) {
        log.info("admin {} delete duanwei : {}", requestParams.getString("adminName"),
            requestParams.getInteger("id"));
        int id = requestParams.getInteger("id");
        duanweiService.deleteByPrimaryKey(id);
        return Response.ok();
    }

    /**
     * 4、上传Duanwei等级相对应的图片
     */
    @PostMapping("/pic/upload")
    public Response uploadDuanweiPic(@RequestBody JSONObject requestParams){
        String name = requestParams.getString("name");
        String picName = requestParams.getString("picName");
        String imgOrigin = requestParams.getString("pic");
        String suffix = picName.substring(picName.lastIndexOf("."));
        String imgName = "duanweiPic/" + name + "_duanwei"+suffix;
        String picUrl;
        try {
            picUrl = obsUtil.uploadPic(imgName,imgOrigin);
        } catch (Exception e) {
            return Response.common(log,"上传段位等级图片失败", ErrorCode.EXTERNAL_ERROR,"上传段位等级图片失败",null);
        }

        return Response.ok(picUrl);
    }

    @Data
    static class ModReqBean {

        String adminName;
        String token;
        List<Duanwei> duanweis;
    }

}
